home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / clx.lha / clx / socket.c < prev    next >
C/C++ Source or Header  |  1988-09-12  |  3KB  |  122 lines

  1. /* Copyright    Massachusetts Institute of Technology    1988    */
  2. /*
  3.  * THIS IS AN OS DEPENDENT FILE! It should work on 4.2BSD derived
  4.  * systems.  VMS and System V should plan to have their own version.
  5.  *
  6.  * This code was cribbed from lib/X/XConnDis.c.
  7.  * Compile using   
  8.  *                    % cc -c socket.c -DUNIXCONN
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <X11/Xos.h>
  13. #include <X11/Xproto.h>
  14. #include <errno.h>
  15. #include <netinet/in.h>
  16. #include <sys/ioctl.h>
  17. #include <netdb.h> 
  18. #include <sys/socket.h>
  19. #ifndef hpux
  20. #include <netinet/tcp.h>
  21. #endif
  22.  
  23. #ifdef UNIXCONN
  24. #include <sys/un.h>
  25. #ifndef X_UNIX_PATH
  26. #define X_UNIX_PATH "/tmp/.X11-unix/X"
  27. #endif /* X_UNIX_PATH */
  28. #endif /* UNIXCONN */
  29. void bcopy();
  30.  
  31. /* 
  32.  * Attempts to connect to server, given host and display. Returns file 
  33.  * descriptor (network socket) or 0 if connection fails.
  34.  */
  35.  
  36. int connect_to_server (host, display)
  37.      char *host;
  38.      int display;
  39. {
  40.   struct sockaddr_in inaddr;    /* INET socket address. */
  41.   struct sockaddr *addr;        /* address to connect to */
  42.   struct hostent *host_ptr;
  43.   int addrlen;            /* length of address */
  44. #ifdef UNIXCONN
  45.   struct sockaddr_un unaddr;    /* UNIX socket address. */
  46. #endif
  47.   extern char *getenv();
  48.   extern struct hostent *gethostbyname();
  49.   int fd;                /* Network socket */
  50.   {
  51. #ifdef UNIXCONN
  52.     if ((host[0] == '\0') || 
  53.     (strcmp("unix", host) == 0)) {
  54.     /* Connect locally using Unix domain. */
  55.     unaddr.sun_family = AF_UNIX;
  56.     (void) strcpy(unaddr.sun_path, X_UNIX_PATH);
  57.     sprintf(&unaddr.sun_path[strlen(unaddr.sun_path)], "%d", display);
  58.     addr = (struct sockaddr *) &unaddr;
  59.     addrlen = strlen(unaddr.sun_path) + 2;
  60.     /*
  61.      * Open the network connection.
  62.      */
  63.     if ((fd = socket((int) addr->sa_family, SOCK_STREAM, 0)) < 0)
  64.         return(-1);        /* errno set by system call. */
  65.     } else
  66. #endif
  67.     {
  68.       /* Get the statistics on the specified host. */
  69.       if ((inaddr.sin_addr.s_addr = inet_addr(host)) == -1) 
  70.     {
  71.       if ((host_ptr = gethostbyname(host)) == NULL) 
  72.         {
  73.           /* No such host! */
  74.           errno = EINVAL;
  75.           return(-1);
  76.         }
  77.       /* Check the address type for an internet host. */
  78.       if (host_ptr->h_addrtype != AF_INET) 
  79.         {
  80.           /* Not an Internet host! */
  81.           errno = EPROTOTYPE;
  82.           return(-1);
  83.         }
  84.       /* Set up the socket data. */
  85.       inaddr.sin_family = host_ptr->h_addrtype;
  86.       bcopy((char *)host_ptr->h_addr, 
  87.         (char *)&inaddr.sin_addr, 
  88.         sizeof(inaddr.sin_addr));
  89.     } 
  90.       else 
  91.     {
  92.       inaddr.sin_family = AF_INET;
  93.     }
  94.       addr = (struct sockaddr *) &inaddr;
  95.       addrlen = sizeof (struct sockaddr_in);
  96.       inaddr.sin_port = display + X_TCP_PORT;
  97.       inaddr.sin_port = htons(inaddr.sin_port);
  98.       /*
  99.        * Open the network connection.
  100.        */
  101.       if ((fd = socket((int) addr->sa_family, SOCK_STREAM, 0)) < 0){
  102.     return(-1);        /* errno set by system call. */}
  103.       /* make sure to turn off TCP coalescence */
  104. #ifdef TCP_NODELAY
  105.       {
  106.     int mi = 1;
  107.     setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &mi, sizeof (int));
  108.       }
  109. #endif
  110.     }
  111.     if (connect(fd, addr, addrlen) == -1) 
  112.       {
  113.     (void) close (fd);
  114.     return(-1);         /* errno set by system call. */
  115.       }
  116.   }
  117.   /*
  118.    * Return the id if the connection succeeded.
  119.    */
  120.   return(fd);
  121. }
  122.